import difflib, time
from loguru import logger
import settings
import math
from queue import Queue
import threading

class LengthException(Exception): message:str = ''

def diff_string(reference, verify_value) -> float:
    """
    字符串相似度比较
    :param reference:  参照字符串
    :param verify_value:  需要验证的字符串
    :return:  float
    """
    diff = difflib.SequenceMatcher(None, reference, verify_value).quick_ratio()
    logger.info('参照值:({})与测试值:({})的相似度为:{}',reference, verify_value, diff)
    return diff

def complete_slice(count:int = 0) -> list:
    """
    对一个数进行切分，去最大区间
    :param count: 长度
    :return:
    """
    # 组数量, 向上去整
    group_count = math.ceil(count / settings.diff_limit or 10)
    group = []
    for i in range(group_count):
        start = i * settings.diff_limit
        end = (i+1) * settings.diff_limit
        if count < end:
            end = count
        group.append((start, end))
    return group

def batch_diff(reference_list:list, verify_list :list):
    """
    批量对比
    :param reference_list:
    :param verify_list:
    :return:
    """
    logger.debug('当前线程：{}', threading.current_thread().name)
    msg = '\n=================== reference_list ===================\n{}\n'
    msg += '==================== verify_list ====================\n{}\n'
    msg += '======================================================='
    logger.debug(msg, reference_list, verify_list)
    if len(reference_list) != len(verify_list):
        logger.error('对比长度不一致！')
        length_error =  LengthException()
        length_error.message = '对比长度不一致！'
        raise length_error
    diff_container = []
    for index in range(len(reference_list)):
        diff = diff_string(reference_list[index], verify_list[index])
        if diff < settings.diff_similarity:
            diff_container.append({
                'reference': reference_list[index],
                'verify': verify_list[index]
            })
    return diff_container

